<?php
class GeryMacroContext extends AbstractGeryContext {
	
	var $macroName;
	
	var $context;
	
	public function GeryMacroContext(IGeryContext $innerContext, $macroName) {
		parent::__construct($innerContext);
		
		$this->macroName = $macroName;
		$this->context = new HashMap();
	}
	
	public function &internalGet($key) {
		return $this->context->get($key);
	}
	
	public function internalPut($key, $value) {
		return $this->context->put($key, $value);
	}
	
	public function internalGetKeys() {
		$this->context->getKeys();
	}
	
	public function internalRemove($key) {
		$this->context->remove($key);
	}
	
	public function internalContainsKey($key) {
		return $this->context->containsKey($key);
	}
	
	public function getTemplate($resourceName) {
		return $this->getHouseKeepingContext()->getTemplate($resourceName);
	}
	
	public function getHouseKeepingContext() {
		$this->getChainedContext()->getHouseKeepingContext();
	}
	
	function getTemplateName() {
		return $this->getChainedContext()->getTemplateName();
	}
	
	public function isMacroRegistered($macroName) {
		return $this->getInternalHouseKeepingContext()->isMacroRegistered($macroName);
	}
	
	function getInternalHouseKeepingContext() {
		return $this->getChainedContext()->getInternalHouseKeepingContext();
	}
	
	function callMacro($macroName, $parameters) {
		return $this->getInternalHouseKeepingContext()->callMacro($macroName, $this, $parameters);	
	}
	
	public function log($message, $lineNumber, $level = LOG_LEVEL_INFO) {
		
		// get the class where this macro is defined
		$className = $this->getInternalHouseKeepingContext()->getClassNameForMacro($this->macroName);
		
		// call getResourceName() of the class
		$templateName = call_user_func(array($className, 'getResourceName'));
		
		// create a new location
		$location = new TemplateLocation($lineNumber, $templateName, $this->macroName);
		
		// log the message
		$this->getLogger()->log($message . ' in ' . $location->__toString(), $level);	
		
		if ($level == LOG_LEVEL_FATAL)
			throw new GeryRuntimeException($message, $location);
	}
	
	/**
	 * Get the engine logger
	 *
	 * @return Logger
	 */
	function getLogger() {
		return $this->getInternalHouseKeepingContext()->getLogger();
	}
}